home *** CD-ROM | disk | FTP | other *** search
- Path: news.halcyon.com!usenet
- From: normanb@halcyon.com (Norm Bryar)
- Newsgroups: comp.lang.c++
- Subject: Re: sizeof dymanic arrays
- Date: Sat, 24 Feb 1996 19:21:35 GMT
- Organization: Northwest Nexus Inc.
- Message-ID: <4gnoer$t4j@news.halcyon.com>
- References: <312BE6BA.3725@byu.edu> <312C9863.372D@bridge.bst.bls.com>
- NNTP-Posting-Host: blv-pm11-ip3.halcyon.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- Just Another Net User <gary.smith3@bridge.bst.bls.com> wrote:
-
- >> I have a problem with getting the size of a dynamic array.
- >>
- >> {
- >> char* temp;
- >> temp=new char[10];
- >> cout << sizeof temp;
- >> }
- >>
- >> I get 4 back for this. Which is the size of the pointer, not the array.
-
- >Correct. Also sizeof *temp returns 1 (the size of 1 char).
-
- >> Does anyone know how to get the number of elements in a dynamic array?
-
- >Yes, you already know. You did tell new how many elements you wanted in
- >the first place (10 in your example).
-
-
- More generally, however, if someone gives you a pointer you did not
- allocate (or realloc), it's not really possible to figure out how big
- the useful portion of that block is.
-
- The Windows API GlobalSize() tells you how big a block was returned,
- which is >= the size requested in GlobalAlloc().
-
- Whoever passes in the pointer must tell you how big the memory is or
- provide some identifiable termination marker in the data. Period.
-
- --Norm
-
-